package org.benben;

import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
import java.text.*;

public class HttpRequest
{
    private String urlString;
    private URL url; 
    private HttpURLConnection urlConnection;
    private int timeOut;
    
    public HttpRequest(String urlString)
    {
        this.urlString = urlString;
        this.timeOut = 1000;
    }
    
    public void setTimeOut(int timeOut)
    {
        this.timeOut = timeOut;
    }
    
    public int getTimeOut()
    {
        return this.timeOut;
    }
    
    public boolean connect()
    {
        boolean result = true;
        
        try
        {
            url = new URL(urlString);
            urlConnection = (HttpURLConnection)url.openConnection();
            urlConnection.setConnectTimeout(1000);
            urlConnection.connect();   
           
            //urlConnection.setReadTimeout(3000);
        }catch(IOException ex)
        {
            ex.printStackTrace();
            result = false;
        }
        finally
        {
            return result;
        }
                
    }
    
    public int getResponseCode()
    {
        int code = -1;
        
        try
        {
            code = urlConnection.getResponseCode();
        }catch(IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            return code;
        }
    }
    
    public String getCotentType()
    {
        String result = urlConnection.getContentType();
        
        if(result == null)
            return "";
        else
            return result;
    }
                
    public String getEncoding()
    {
        String encoding = null;
        
        encoding = urlConnection.getContentEncoding();
        
        if(encoding != null)
            return encoding;
        
        String content = getCotentType();
        int index = content.indexOf("charset");
        if(index < 0)
            return "";
        
        content = content.substring(index, content.length());
        String[] temp = content.split("=");
        encoding = temp[1].trim();
        
        return encoding;
    }
    
    public int getContentLength()
    {
        return urlConnection.getContentLength();
    }
    
    public String getLastModified()
    {
        long lastModified = urlConnection.getLastModified();
        
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = simpleDateFormat.format(new Long(lastModified));
        
        return result;
    }
    
    public long getLastModifiedLong()
    {
        return urlConnection.getLastModified();
    }
    
    public String getContent()
    {
        String result = "";
        
        try
        {
            result = urlConnection.getContent().toString();
        }catch(IOException ex)
        {
            ex.printStackTrace();
            result = "";
        }
        finally
        {
            return result;
        }
    }
    
    public String getURL()
    {
        //String result = urlConnection.getURL().toString().trim();
        
        //if(result == null || result.equals("") == true)
        //    return urlString;
        //else
        //    return result;
        
        return this.urlString;
    }
    
    /**
     * 
     *@param  werwrewr
     * @return  werwes23
     */
    public String getURLContent()
    {
        String result = "";
        StringBuilder stringBuilder = new StringBuilder();
        String line = "";
        String contentEncoding = getEncoding();
        System.out.println("encoding:" + contentEncoding);
        if(contentEncoding == null || contentEncoding.equals("") == true)
            contentEncoding = "utf-8";
        try
        {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), contentEncoding));
            long start = Utils.getTimer();
            while((line = bufferedReader.readLine()) != null)
            {   
                stringBuilder.append(line + "\n");
                long current = Utils.getTimer();
                if((current - start) > this.timeOut)
                {
                    throw new SocketTimeoutException();
                }
            }
        }catch(java.net.SocketTimeoutException ex)
        {
            ex.printStackTrace();
            result = "";
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
            stringBuilder = new StringBuilder();
        }
        finally
        {
            result = stringBuilder.toString().trim();
            //System.out.println(result);
            return result;
        }
    }
}

